package com.staginfo.segs;

import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.staginfo.segs.base.BaseActivity;
import com.staginfo.segs.common.Constants;
import com.staginfo.segs.ui.fragment.HomeFragment;
import com.staginfo.segs.ui.fragment.ManagerFragment;
import com.staginfo.segs.ui.fragment.PersonalFragment;
import com.staginfo.segs.ui.fragment.map.ConvertMapListenner;
import com.staginfo.segs.util.ToastUtil;

import butterknife.Bind;
import butterknife.OnClick;


public class MainActivity extends BaseActivity implements BottomNavigationBar.OnTabSelectedListener, ConvertMapListenner {

    @Bind(R.id.title)
    TextView mTitle;
    @Bind(R.id.toolbar)
    Toolbar mToolbar;
    @Bind(R.id.tv_mind_warning)
    TextView mTvMindWarning;
    @Bind(R.id.tv_all_warning)
    TextView mTvAllWarning;


    @Override
    public int getLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        super.initView();
        initToolBar(); // 标题栏
        initBottomNavBar();
        initTab();

    }

    private void initToolBar() {
        mTitle.setText(R.string.bottom_nav_home);
        mTvMindWarning.setSelected(true);
    }

    @Override
    public void initData() {
        super.initData();
        bottomNavigationBar.setTabSelectedListener(this);
    }


    private BottomNavigationBar bottomNavigationBar;
    private HomeFragment homeFragment;
    private ManagerFragment mangerFragment;
    private PersonalFragment personFragment;


    private void initBottomNavBar() { /*1.首先进行fvb*/
        bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_nav_bar); /*2.进行必要的设置*/
        bottomNavigationBar.setBarBackgroundColor(R.color.gray);   // 导航栏背景颜色
        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);//固定大小

/*        BadgeItem badgeItem = new BadgeItem()
                .setBackgroundColor(Color.RED) // 通知背景颜色
                .setText("5")
                .setHideOnSelect(true);// 选中时显示，这个可自动调整*/

         /*3.添加Tab*/
        bottomNavigationBar
                .addItem(new BottomNavigationItem(R.drawable.warning_select_icon, R.string.bottom_nav_home)/*.setBadgeItem(badgeItem)*/
                        .setInactiveIconResource(R.drawable.warning_default_icon).setActiveColorResource(R.color.littleblue))

                .addItem(new BottomNavigationItem(R.drawable.manager_select_icon, R.string.bottom_nav_manager)
                        .setInactiveIconResource(R.drawable.manager_default_icon).setActiveColorResource(R.color.littleblue))

                .addItem(new BottomNavigationItem(R.drawable.personal_select_icon, R.string.bottom_nav_person)
                        .setInactiveIconResource(R.drawable.personal_default_icon).setActiveColorResource(R.color.littleblue))
                .setFirstSelectedPosition(0) //默认显示面板
                .initialise();  //初始化
    }


    @Override
    public void onTabSelected(int position) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        switch (position) {
            case 0:
                if (homeFragment == null) {
                    homeFragment = new HomeFragment();
                }
                mTitle.setText(R.string.bottom_nav_home);
                transaction.replace(R.id.content_main, homeFragment);
                //显示告警标题文字
                showWarningTitle();
                break;
            case 1:
                if (mangerFragment == null) {
                    mangerFragment = new ManagerFragment();
                }
                mTitle.setText(R.string.bottom_nav_manager);
                transaction.replace(R.id.content_main, mangerFragment);
                //隐藏告警标题文字
                hiddleWarningTittle();
                break;
            case 2:
                if (personFragment == null) {
                    personFragment = new PersonalFragment();
                }
                mTitle.setText(R.string.bottom_nav_person);
                transaction.replace(R.id.content_main, personFragment);
                //隐藏告警标题文字
                hiddleWarningTittle();
                break;
        }
        transaction.commit();
    }

    private void showWarningTitle() {
        mTvAllWarning.setVisibility(View.VISIBLE);
        mTvMindWarning.setVisibility(View.VISIBLE);
    }

    private void hiddleWarningTittle() {
        mTvAllWarning.setVisibility(View.GONE);
        mTvMindWarning.setVisibility(View.GONE);
    }


    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {

    }


    private void initTab() {
        homeFragment = new HomeFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.content_main, homeFragment);
        transaction.commit();
    }


    @Override
    public void transToMapFragment() {

    }

    @OnClick({R.id.tv_mind_warning, R.id.tv_all_warning})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_mind_warning:
                ToastUtil.getInstance(this).shortToast("个人告警");
                //改变字体颜色
                mTvMindWarning.setSelected(true);
                mTvAllWarning.setSelected(false);
                //切换fragment
                homeFragment.setWarningType(Constants.WARNING_TYPE_PER);
                break;
            case R.id.tv_all_warning:
                ToastUtil.getInstance(this).shortToast("全部告警");
                //切改变字体颜色
                mTvMindWarning.setSelected(false);
                mTvAllWarning.setSelected(true);
                //切换fragment
                homeFragment.setWarningType(Constants.WARNING_TYPE_ALL);
                break;
        }
    }
}
